package st.business.tepth.business.user.busnumber.workholder.maintain.pass;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

import st.business.tepth.R;
import st.business.tepth.base.OnItemChooseListener;
import st.business.tepth.business.bean.MaintainPassInfoBean;
import st.business.tepth.config.ItemFields;
import st.shadow.library.ui.recycler.MultipleItemEntity;

/**
 * @author zhangyu
 * @date 2019/4/19
 */
public class ClassificationAdapter extends BaseQuickAdapter<MultipleItemEntity, BaseViewHolder> {
    private int mOldPosition = 0;
    private OnItemChooseListener<Integer> mItemChooseListener;
    private Drawable mDrawableDone;

    public ClassificationAdapter(Context context,
                                 int layoutResId,
                                 @Nullable List<MultipleItemEntity> data,
                                 OnItemChooseListener<Integer> listener) {
        super(layoutResId, data);
        mItemChooseListener = listener;
        mDrawableDone = context.getResources().getDrawable(R.drawable.ic_maintain_done);
        mDrawableDone.setBounds(0, 0,
                mDrawableDone.getMinimumWidth(), mDrawableDone.getMinimumHeight());
    }

    @Override
    protected void convert(final BaseViewHolder viewHolder, final MultipleItemEntity entity) {
        final TextView tvName = viewHolder.getView(R.id.tv_name);
        final MaintainPassInfoBean maintainPassInfoBean = entity.getField(ItemFields.ITEM_OBJ);
        tvName.setCompoundDrawables(maintainPassInfoBean.getCheckStatus() == 1 ?
                        null : mDrawableDone,
                null, null, null);
        tvName.setText(maintainPassInfoBean.getVehicleSeriesName());
        if (!(Boolean) entity.getField("isSelected")) {
            viewHolder.setBackgroundColor(R.id.layout_container,
                    Color.parseColor("#f2f6f9"));
        } else {
            viewHolder.setBackgroundColor(R.id.layout_container,
                    Color.parseColor("#ffffff"));
        }
        tvName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOldPosition != viewHolder.getAdapterPosition()) {
                    entity.setField("isSelected", true);
                    viewHolder.setBackgroundColor(R.id.layout_container,
                            Color.parseColor("#ffffff"));
                    MultipleItemEntity old = getItem(mOldPosition);
                    if (old != null) {
                        old.setField("isSelected", false);
                        notifyItemChanged(mOldPosition);
                    }
                    mOldPosition = viewHolder.getAdapterPosition();
                    //回调
                    if (mItemChooseListener != null) {
                        mItemChooseListener.onChoose(viewHolder.getAdapterPosition());
                    }
                }
            }
        });
    }

    public List<MultipleItemEntity> getUnPassList() {
        List<MultipleItemEntity> data = getData();
        List<MultipleItemEntity> unPassList = new ArrayList<>();
        for (MultipleItemEntity entity : data) {
            MaintainPassInfoBean maintainPassInfoBean = entity.getField(ItemFields.ITEM_OBJ);
            if (maintainPassInfoBean.getCheckStatus() == 1) {
                unPassList.add(entity);
            }
        }
        return unPassList;
    }
}
